package org.example.streamAPI;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestCollection {
    public static void main(String[] args) {
        testForEach();
        testRemoveIf();
        testStream();
        testParallelStream();
    }

    //TODO 1、forEach
    public static void testForEach() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        //TODO 使用for循环
        for (String s : list) {
            System.out.println(s);
        }

        //TODO 使用lambda表达式
        list.forEach(System.out::println);
    }

    //TODO 2、使用removeIf
    public static void testRemoveIf() {
        List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));

        numbers.removeIf(n -> n % 2 == 0); // 移除所有偶数

        System.out.println(numbers);
    }

    //TODO 3、使用stream
    public static void testStream() {
        List<String> list = Arrays.asList("aaa", "bbb", "ccc","abc");
        List<String> list1=list.stream().filter(s->s.startsWith("a")).collect(Collectors.toList());
        list1.forEach(System.out::println);
    }

    //TODO 4、使用parallelStream
    public static void testParallelStream() {
        List<String> list = Arrays.asList("a1", "a2", "a3","a4","a5","bb","cc");
        list.parallelStream().forEach(System.out::println);
    }

}
